import {
  baseURL
} from '../../../../services/request';
import {
  addActive,
  updateActive,
  getActiveDetail
} from '../../../../services/activePlan/index';
import Toast from 'tdesign-miniprogram/toast/index';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    datetimeVisible: false,
    content: '',
    memo: '',
    pics: '',
    datetimeText: '',
    minDate: new Date(2023, 1, 1).getTime(),
    title: '',
    kid: '',
    gridConfig: {
      column: 4,
      width: 160,
      height: 160,
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      kid
    } = options;
    if (kid) {
      this.setData({
        kid,
      });
      this.getDetails(kid);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  handleCalendar() {
    this.setData({
      datetimeVisible: true,
    });
  },
  onClose() {
    this.setData({
      datetimeVisible: false,
    });
  },
  handleConfirm(e) {
    const {
      value
    } = e.detail;
    const format = (val) => {
      const now = new Date(); // 获取当前时间

      const date = new Date(val);
      const MM = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
      const DD = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
      // const hour = now.getHours(); // 获取小时
      // const minute = now.getMinutes(); // 获取分钟
      // const second = now.getSeconds(); // 获取秒数
      return `${date.getFullYear()}-${MM}-${DD}`;
      // return `${date.getFullYear()}-${MM}-${DD}-${hour}:${minute}:${second}`;
    };
    this.setData({
      datetimeText: format(value),
    });
  },
  onInputcontentValue(e) {
    const {
      value
    } = e.detail;
    this.setData({
      content: value,
    });
  },
  onInputmemoValue(e) {
    const {
      value
    } = e.detail;
    this.setData({
      memo: value,
    });
  },
  onInputtitleValue(e) {
    const {
      value
    } = e.detail;
    this.setData({
      title: value,
    });
  },
  // 图片上传
  handleSuccess(e) {
    const self = this;
    const role = wx.getStorageSync('role');
    const fileList = e.detail.files;
    this.setData({
      pics: fileList[0].url,
    });
    //  多图统一上传
    wx.uploadFile({
      url: `${baseURL}fileserver/pt/fileserver/action/upload`,
      filePath: fileList[0].url,
      name: 'file',
      header: {
        devType: 4,
        userId: role.userId,
        token: role.tokenId,
        appId: 'crm_agent',
        authType: 'agent',
      },
      success: function (result) {
        if (JSON.parse(result.data).code == 200) {
          const resourceUrl = JSON.parse(result.data).data.urlPath;
          self.setData({
            pics: resourceUrl,
          });
        }
      },
    });
  },
  // 移除图片
  handleRemove() {
    this.setData({
      pics: '',
    });
  },
  getDetails(kid) {
    getActiveDetail({
      kid
    }).then((res) => {
      if (res.status) {
        const {
          content,
          memo,
          pics,
          planTime,
          title
        } = res.data;
        this.setData({
          content,
          memo,
          pics,
          datetimeText: planTime,
          title,
        });
      }
    });
  },
  formSubmit() {
    const {
      content,
      memo,
      pics,
      datetimeText,
      title,
      kid
    } = this.data;
    if (!content) {
      Toast({
        selector: '#t-toast',
        message: '请填写内容',
        icon: '',
        duration: 1000,
      });
      return;
    }
    if (!memo) {
      Toast({
        selector: '#t-toast',
        message: '请填写主题',
        icon: '',
        duration: 1000,
      });
      return;
    }
    if (!title) {
      Toast({
        selector: '#t-toast',
        message: '请填写标题',
        icon: '',
        duration: 1000,
      });
      return;
    }

    if (!datetimeText) {
      Toast({
        selector: '#t-toast',
        message: '请填写活动策划时间',
        icon: '',
        duration: 1000,
      });
      return;
    }
    if (!pics) {
      Toast({
        selector: '#t-toast',
        message: '请上传图片',
        icon: '',
        duration: 1000,
      });
      return;
    }
    if (kid) {
      updateActive({
        content,
        memo,
        pics,
        planTime: datetimeText,
        title,
        kid,
      }).then((res) => {
        if (res.status) {
          wx.showToast({
            title: '编辑成功',
            icon: 'none',
            duration: 2000,
          });
        }
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          });
        }, 200);
      });
    } else {
      addActive({
        content,
        memo,
        pics,
        planTime: datetimeText,
        title,
      }).then((res) => {
        if (res.status) {
          wx.showToast({
            title: '创建成功',
            icon: 'none',
            duration: 2000,
          });
        }
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          });
        }, 200);
      });
    }
  },
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});